package sopraim.model;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import sopraim.entity.Location;
import sopraim.entity.Status;
import sopraim.entity.User;

public class AdminDAO {
    
    private SessionFactory factory;
    
    public AdminDAO(SessionFactory factory) {
        this.factory = factory;
    }
    
    public void addUser(String username, String passwd, String email, boolean isAdmin) {
        
        Session  session = factory.getCurrentSession();
        session.beginTransaction();
        User u = new User();
        u.setUsername(username);
        u.setPassword(passwd);
        u.setIsAdmin(isAdmin);
        u.setMail(email);
        Set friends = new HashSet();
        u.setFriends(friends);
        u.setStatus(new Status("offline"));
        //  session.persist(u); A voir si il y en a besoin.
        session.save(u);
        session.getTransaction().commit();
        
    }
    
    
    public void addUser(String username, String name, String firstname ,String passwd, String email, boolean isAdmin) {
        
        Session  session = factory.getCurrentSession();
        session.beginTransaction();
        User u = new User();
        u.setUsername(username);
        u.setPassword(passwd);
        u.setIsAdmin(isAdmin);
        u.setMail(email);
        u.setName(name);
        u.setFirstName(firstname);
        u.setStatus(new Status("offline"));
        session.save(u);
        session.getTransaction().commit();
        
    }
    
    public void addLocation(String name) {
        
        Session  session = factory.getCurrentSession();
        session.beginTransaction();
        Location l = new Location();
        l.setName(name);
        session.save(l);
        session.getTransaction().commit();
        
    }
    
    public  void removeUser(User user) {
        Session  session = factory.getCurrentSession();
        session.beginTransaction();
        session.delete(user);
        session.getTransaction().commit();
        
    }
    
    public  void addAdminRight(User user) {
        Session  session = factory.getCurrentSession();
        session.beginTransaction();
        user.setIsAdmin(true);
        session.update(user);
        session.getTransaction().commit();
    }
    
    public  void removeAdminRight(User user) {
        Session  session = factory.getCurrentSession();
        session.beginTransaction();
        user.setIsAdmin(false);
        session.update(user);
        session.getTransaction().commit();
    }
    
    
}
